
package com.android.settings.audio.peq.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * @author Wuyexiong
 */
public class NetWorkUtils
{
    private static final String TAG = NetworkInfo.class.getSimpleName();

    public static volatile int netWorkTypeState = 0;

    /**
     * 无网络
     */
    public static final int NETWORK_TYPE_NONE = -1;

    /**
     * WIFI
     */
    public static final int NETWORK_TYPE_WIFI = 1;

    /**
     * 运营商信号
     */
    public static final int NETWORK_TYPE_MOBILE = 2;

    // /**
    // * GPRS
    // */
    // public static final int NETWORK_TYPE_GPRS = 3;

    // /**
    // * 3G
    // */
    // public static final int NETWORK_TYPE_3G = 4;

    /**
     * 未知网络-已链接
     */
    public static final int NETWORK_TYPE_UNKNOW = 5;

    /**
     * 是否已经开启网络链接
     * 
     * @param context
     * @return
     */
    public static boolean isConnected(Context context)
    {
        try
        {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
            if (aliveInfo != null && aliveInfo.isConnected())
            {
                return true;
            }
        } catch (Exception e)
        {
            return false;
        }
        return false;
    }

    /**
     * 当前网络状态是否已链接上WIFI
     * 
     * @param context
     * @return
     */
    public static boolean isWifi(Context context) {
        try
        {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
            if (aliveInfo != null && aliveInfo.isConnected())
            {
                // LogUtil.d(TAG, "TypeName : " + aliveInfo.getTypeName());
                // LogUtil.d(TAG, "Type : " + aliveInfo.getType());
                // LogUtil.d(TAG, "Subtype : " + aliveInfo.getSubtype());
                // LogUtil.d(TAG, "SubtypeName : " +
                // aliveInfo.getSubtypeName());
                if (aliveInfo.getType() == ConnectivityManager.TYPE_WIFI)
                {
                    return true;
                }
            }
        } catch (Exception e)
        {
            return false;
        }
        return false;
    }

    /**
     * 当前网络是否已经链接上运营商网络
     * 
     * @param context
     * @return
     */
    public static boolean isMobile(Context context)
    {
        try
        {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
            if (aliveInfo != null && aliveInfo.isConnected())
            {
                // LogUtil.d(TAG, "TypeName : " + aliveInfo.getTypeName());
                // LogUtil.d(TAG, "Type : " + aliveInfo.getType());
                // LogUtil.d(TAG, "Subtype : " + aliveInfo.getSubtype());
                // LogUtil.d(TAG, "SubtypeName : " +
                // aliveInfo.getSubtypeName());
                if (aliveInfo.getType() == ConnectivityManager.TYPE_MOBILE)
                {
                    // LogUtil.d(TAG, "" + aliveInfo.getTypeName());
                    return true;
                }
            }
        } catch (Exception e)
        {
            return false;
        }
        return false;
    }

    /**
     * @param context
     * @return
     */
    public static boolean is3G(Context context)
    {
        boolean result = false;
        try
        {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
            if (aliveInfo != null && aliveInfo.isConnected())
            {
                // LogUtil.d(TAG, "TypeName : " + aliveInfo.getTypeName());
                // LogUtil.d(TAG, "Type : " + aliveInfo.getType());
                // LogUtil.d(TAG, "Subtype : " + aliveInfo.getSubtype());
                // LogUtil.d(TAG, "SubtypeName : " +
                // aliveInfo.getSubtypeName());

                TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                // LogUtil.d(TAG, "NetworkType : " + manager.getNetworkType());
                // if(manager.getNetworkType() ==
                // TelephonyManager.NETWORK_TYPE_UMTS)
                // {
                // // LogUtil.d(TAG, "" + manager.getNetworkOperatorName());
                // // LogUtil.d(TAG, "" + manager.getNetworkOperator());
                // // LogUtil.d(TAG, "" + manager.getNeighboringCellInfo());
                // // LogUtil.d(TAG, "" + manager.getNetworkCountryIso());
                // return true;
                // }

                int type = manager.getNetworkType();
                switch (type) {
                // 联通3g
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                        // 电信3g
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        result = true;
                        break;
                    default:
                        break;
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param context
     * @return
     */
    public static boolean is2G(Context context) {
        boolean result = false;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
            if (aliveInfo != null && aliveInfo.isConnected()) {
                TelephonyManager manager = (TelephonyManager) context
                        .getSystemService(Context.TELEPHONY_SERVICE);
                int type = manager.getNetworkType();
                switch (type) {
                // 移动或联通 2g
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                        // 电信2g
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                        result = true;
                        break;
                    default:
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param context
     * @return
     */
    public static boolean isGRPS(Context context)
    {
        try
        {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
            if (aliveInfo != null && aliveInfo.isConnected())
            {
                TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                // LogUtil.d(TAG, "TypeName : " + aliveInfo.getTypeName());
                // LogUtil.d(TAG, "Type : " + aliveInfo.getType());
                // LogUtil.d(TAG, "Subtype : " + aliveInfo.getSubtype());
                // LogUtil.d(TAG, "SubtypeName : " +
                // aliveInfo.getSubtypeName());

                if (manager.getNetworkType() == TelephonyManager.NETWORK_TYPE_GPRS)
                {
                    // LogUtil.d(TAG, "" + manager.getNetworkOperatorName());
                    return true;
                }
            }
        } catch (Exception e)
        {
            return false;
        }
        return false;
    }
    /**
     * @param context
     * @return 0 标识无网络，1 标识移动网络，2标识wifi,3标识2G,4标识3G,5标识4G
     */
    public static int getNetworkType(Context context) {
//        String strNetworkType = "";
        int network = 0;

        NetworkInfo networkInfo = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
//                strNetworkType = "WIFI";
                network = 2;
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                String _strSubTypeName = networkInfo.getSubtypeName();


                // TD-SCDMA   networkType is 17
                int networkType = networkInfo.getSubtype();
                switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
//                        strNetworkType = "2G";
                        network = 3;
                        break;
                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                    case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                    case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
//                        strNetworkType = "3G";
                        network = 4;
                        break;
                    case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
//                        strNetworkType = "4G";
                        network = 5;
                        break;
                    default:
                        // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                        if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
//                            strNetworkType = "3G";
                            network = 4;
                        } else {
//                            strNetworkType = _strSubTypeName;
                            network = 1;
                        }

                        break;
                }

            }
        }
        return network;
    }
    /**
     * 获取当前的网络状态
     * 
     * @param context
     * @return
     */
    public static int checkNetWorkState(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
        if (aliveInfo != null && aliveInfo.isConnected()) {
            if (aliveInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                NetWorkUtils.netWorkTypeState = NETWORK_TYPE_WIFI;
            } else if (aliveInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                NetWorkUtils.netWorkTypeState = NETWORK_TYPE_MOBILE;
            } else {
                NetWorkUtils.netWorkTypeState = NETWORK_TYPE_UNKNOW;
            }
        } else {
            NetWorkUtils.netWorkTypeState = NETWORK_TYPE_NONE;
        }
        return NetWorkUtils.netWorkTypeState;
    }

    // /**
    // * 调用此方法的有效前提是调用过了checkNetWorkState()这个函数
    // * @return
    // */
    // public static boolean isGPRS()
    // {
    // return NetWorkUtils.netWorkTypeState == NETWORK_TYPE_GPRS;
    // }

    /**
     * 调用此方法的有效前提是调用过了checkNetWorkState()这个函数
     * 
     * @return
     */
    public static boolean isWifi()
    {
        return NetWorkUtils.netWorkTypeState == NETWORK_TYPE_WIFI;
    }

    // /**
    // * 调用此方法的有效前提是调用过了checkNetWorkState()这个函数
    // * @return
    // */
    // public static boolean is3G()
    // {
    // return NetWorkUtils.netWorkTypeState == NETWORK_TYPE_3G;
    // }

    /**
     * 调用此方法的有效前提是调用过了checkNetWorkState()这个函数
     * 
     * @return
     */
    public static boolean isOtherState()
    {
        return NetWorkUtils.netWorkTypeState == NETWORK_TYPE_UNKNOW;
    }

    public static boolean isMobile()
    {
        return NetWorkUtils.netWorkTypeState == NETWORK_TYPE_MOBILE;
    }

    /**
     * @return 得到当前网络状态,调用此方法的有效前提是调用过了checkNetWorkState()这个函数
     */
    public synchronized static int getNetWorkState()
    {
        return NetWorkUtils.netWorkTypeState;
    }

    public static void setNetWorkState(int netWorkState)
    {
        NetWorkUtils.netWorkTypeState = netWorkState;
    }

    /**
     * 无网络状态,调用此方法的有效前提是调用过了checkNetWorkState()这个函数
     * 
     * @return
     */
    public static boolean isNetWorkStateNone()
    {
        return NetWorkUtils.netWorkTypeState == NETWORK_TYPE_NONE;
    }

    public static String getNetStateDec()
    {
        String result = "";
        switch (netWorkTypeState) {
            case 1:
                result = "wifi";
                return result;
            case 2:
                result = "X";
                return result;
            case 3:
                result = "gprs";
                return result;
            case 4:
                result = "3G";
                return result;
            default:
                break;
        }
        return result;
    }

    /**
     * 登录接口使用，网络类型
     * 1 wifi
     * 2 gprs
     * 3 3G
     * 4 4G
     * 5 pc网络
     * @param context
     * @return
     */
    public static int getLoginNetwork(Context context) {
        int netType = 0;
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo aliveInfo = connectivityManager.getActiveNetworkInfo();
            if (aliveInfo != null && aliveInfo.isConnected()) {
                if (aliveInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    NetWorkUtils.netWorkTypeState = NETWORK_TYPE_WIFI;
                    netType = 1;
                } else if (aliveInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    NetWorkUtils.netWorkTypeState = NETWORK_TYPE_MOBILE;
                    TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                    switch (manager.getNetworkType()) {
                        case TelephonyManager.NETWORK_TYPE_GPRS:
                            //case TelephonyManager.NETWORK_TYPE_GSM:
                        case TelephonyManager.NETWORK_TYPE_EDGE:
                        case TelephonyManager.NETWORK_TYPE_CDMA:
                        case TelephonyManager.NETWORK_TYPE_1xRTT:
                        case TelephonyManager.NETWORK_TYPE_IDEN:
                            // NETWORK_CLASS_2_G;
                            netType = 2;
                            break;
                        case TelephonyManager.NETWORK_TYPE_UMTS:
                        case TelephonyManager.NETWORK_TYPE_EVDO_0:
                        case TelephonyManager.NETWORK_TYPE_EVDO_A:
                        case TelephonyManager.NETWORK_TYPE_HSDPA:
                        case TelephonyManager.NETWORK_TYPE_HSUPA:
                        case TelephonyManager.NETWORK_TYPE_HSPA:
                        case TelephonyManager.NETWORK_TYPE_EVDO_B:
                        case TelephonyManager.NETWORK_TYPE_EHRPD:
                        case TelephonyManager.NETWORK_TYPE_HSPAP:
                            // NETWORK_CLASS_3_G;
                            netType = 3;
                            break;
                        case TelephonyManager.NETWORK_TYPE_LTE:
                            // NETWORK_CLASS_4_G;
                            netType = 4;
                            break;
                        default:
                            // NETWORK_CLASS_UNKNOWN;
                            break;
                    }
                } else {
                    NetWorkUtils.netWorkTypeState = NETWORK_TYPE_UNKNOW;
                }
            } else {
                NetWorkUtils.netWorkTypeState = NETWORK_TYPE_NONE;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return netType;
    }

}
